﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using S1882I;
using System.IO;

namespace card
{
    public partial class Frm_readcard : Form
    {
        S1882IReWr rw = new S1882IReWr();
        public static WebDG.Service ws = new card.WebDG.Service();
        string shipname = "";
        string cargoname = "";
        string shipper_name = "";
        string setowner = "";
        string yard = "";
        string stackname = "";
        //string cargono = "0";

        //string logintime = "";
        string ioyardid = "0";//市入//待确认0或1
        string str_operator = "";
        string plan_code = "0";//无计划作业

        string truckno = "";
        string truckcolor = "";
        string drivername = "";
        string driverid = "";
        string driverphone = "";

        string pieces = "";
        string weight = "";
        string standards = "";
        string bzbz = "";

        //string cardadd = "东门";
        string cardadd="";

        //string str_curdate = "";
        string cardtime = "";
        public string io_port
        {
            get;
            set;
        }

        public Frm_readcard()
        {
            InitializeComponent();
        }

        private void Frm_readcard_Load(object sender, EventArgs e)
        {
            //connect_card();
            //FileStream aFile = new FileStream(@"C:\truck_login_id.ini", FileMode.Open);
            //StreamReader sr = new StreamReader(aFile);//用FileStream对象实例化一个StreamReader对象,若全部为英文，可用这种方式
            //string strLine1 = sr.ReadLine();//   读取一行字符并返回
            //sr.Close();
            //if (strLine1 == "0")
            //{
            //    connect_card();
            //}


            //connect_card();
            str_operator = this.Text.Substring(2);
        }

        private void connect_card()
        {
            short by = rw.Connect_Open(io_port);
            if (by == 0)
            {
                //MessageBox.Show (DateTime.Now + "  提示:连接读写器成功.\r\n");
                //write_ini();
            }
            else
            {
                MessageBox.Show(DateTime.Now + String.Format("  错误:读写器连接失败:{0:X}.", by) + "\r\n");
            }
            //rtLog.ScrollToCaret(); 
        }

        private int WRITE_TRUCKINFO(string info)
        {
            connect_card();
            int i_return = 0;
            string data = ConvertTools.ToHex(info, "UTF-8");
            //short by = rw.WriteUser(data);
            short by = rw.WriteUser(data);
            if (by == 0)
                i_return = 1;
            //MessageBox.Show(DateTime.Now + "  写入车辆信息成功！\r\n");
            //byte[] tagsid = rw.ReadID().tags_id;
            else
                i_return = 0;
            //MessageBox.Show(DateTime.Now + String.Format("  写入车辆信息失败:{0:X}.", by) + "\r\n");
            //rtLog.ScrollToCaret();
            disconnect_card();
            return i_return;
        }
        private int write_truck_epcid(string info)
        {
            connect_card();
            int i_return = 0;
            string data = info;
            short by = rw.WriteID(data);
            if (by == 0)
                i_return = 1;
            //MessageBox.Show(DateTime.Now + "  写入EPCID成功！\r\n");
            //byte[] tagsid = rw.ReadID().tags_id;
            else
                i_return = 0;
            //MessageBox.Show(DateTime.Now + String.Format("  写入EPCID失败:{0:X}.", by) + "\r\n");
            //rtLog.ScrollToCaret();
            disconnect_card();
            return i_return;
        }
        private string read_truck_epcid()
        {
            connect_card();
            string str_return = "";
            TagCountId tc = rw.ReadID();
            if (tc.iDBitCount > 0)
            {
                string strid = "";
                for (int j = 1; j <= tc.tags_id[0]; j++)
                {
                    strid += String.Format("{0:X02}", tc.tags_id[j]);
                }
                str_return = strid.Substring(6);
                //rtLog.AppendText(DateTime.Now + "  读到的标签:" + strid + "\r\n");
            }
            else
                str_return = "-1";
            //rtLog.AppendText(DateTime.Now + " 读标签失败！" + "\r\n");
            //rtLog.ScrollToCaret(); 
            disconnect_card();
            return str_return;
        }
        private string read_truckinfo()
        {
            connect_card();
            string str_return = "";
            TagCountId tc = rw.ReadUser();
            if (tc.iDBitCount > 0)
            {
                string strid = "";
                for (int j = 1; j <= tc.tags_id[0]; j++)
                {
                    strid += String.Format("{0:X02}", tc.tags_id[j]);
                }
                strid = ConvertTools.UnHex(strid, "UTF-8");
                str_return = strid;
                //rtLog.AppendText(DateTime.Now + "  读到的标签:" + ConvertTools.UnHex(strid, "UTF-8") + "\r\n");
            }
            else
                str_return = "-1";
            //rtLog.AppendText(DateTime.Now + " 读标签失败！" + "\r\n");
            //rtLog.ScrollToCaret();
            disconnect_card();
            return str_return;
        }
        private void disconnect_card()
        {
            short by = rw.Connect_Close();
            if (by == 0)
            { }
            //  rtLog.AppendText(DateTime.Now + "  提示:读写器断开成功.\r\n");
            else
            {
                MessageBox.Show(DateTime.Now + String.Format("  错误:读写器连接断开失败:{0:X}.", by) + "\r\n");
            }

            //rtLog.ScrollToCaret(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str_truckinfo_temp = read_truckinfo();
            int I_TEMP = 0;
            if (str_truckinfo_temp == "-1" || str_truckinfo_temp.Length > 10)
            {
                while (I_TEMP < 10)
                {
                    str_truckinfo_temp = read_truckinfo();
                    if (str_truckinfo_temp == "-1" || str_truckinfo_temp.Length > 10)
                    {
                        I_TEMP = I_TEMP + 1;
                    }
                    else
                    {
                        break;
                    }
                }
                if (I_TEMP == 10)
                {
                    MessageBox.Show("该卡为空卡！，无信息。1" + str_truckinfo_temp);
                    return;
                }
            }
            

            //MessageBox.Show(str_truckinfo_temp.Length.ToString ());
            ioyardid = str_truckinfo_temp.Substring(0,1);
            truckno = str_truckinfo_temp.Substring(2,7);            

            if (ioyardid == "0")
            {
                string sqlStr = "select drivername,driverid,driverphone,stackname,cargoname,shipper_name,yard,pieces,weight,standards,truckcolor,cardtime,cardadd,plan_code,shipname,setowner from truck_login where TRUCKNO='" + truckno + "' AND IOYARDID='" + ioyardid + "'";
                //MessageBox.Show(StrSql);
                DataSet ds = ws.getDataSet_Serv(sqlStr);
                //DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sqlStr.ToString())).Tables[0];
                //DataSet ds = db.ExecuteDataSet(db.GetSqlStringCommand(sqlStr.ToString()));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    drivername = ds.Tables[0].Rows[0][0].ToString();
                    driverid = ds.Tables[0].Rows[0][1].ToString();
                    driverphone = ds.Tables[0].Rows[0][2].ToString();
                    stackname = ds.Tables[0].Rows[0][3].ToString();
                    cargoname = ds.Tables[0].Rows[0][4].ToString();
                    shipper_name = ds.Tables[0].Rows[0][5].ToString();
                    yard = ds.Tables[0].Rows[0][6].ToString();
                    pieces = ds.Tables[0].Rows[0][7].ToString();
                    weight = ds.Tables[0].Rows[0][8].ToString();
                    standards = ds.Tables[0].Rows[0][9].ToString();
                    truckcolor = ds.Tables[0].Rows[0][10].ToString();
                    cardtime = ds.Tables[0].Rows[0][11].ToString();
                    cardadd = ds.Tables[0].Rows[0][12].ToString();
                    plan_code = ds.Tables[0].Rows[0][13].ToString();
                    shipname = ds.Tables[0].Rows[0][14].ToString();
                    setowner = ds.Tables[0].Rows[0][15].ToString();
                    

                    txt_truckno.Text = truckno;
                    txt_truckcolor.Text = truckcolor;
                    txt_id.Text = driverid;
                    txt_phone.Text = driverphone;
                    txt_drivername.Text = drivername;

                    txt_bzbz.AppendText("货名：" + cargoname + "\r\n");
                    txt_bzbz.AppendText("船名：" + shipname  + "\r\n");
                    txt_bzbz.AppendText("垛名：" + stackname + "\r\n");
                    txt_bzbz.AppendText("货主：" + shipper_name + "\r\n");
                    txt_bzbz.AppendText("发货人：" + setowner  + "\r\n");
                    txt_bzbz.AppendText("件数：" + pieces  + "\r\n");
                    txt_bzbz.AppendText("重量：" + weight  + "\r\n");
                    txt_bzbz.AppendText("规格：" + standards  + "\r\n");
                    txt_bzbz.AppendText("作业场区：" + yard + "\r\n");
                    txt_bzbz.AppendText("写卡时间：" + cardtime  + "\r\n");
                    txt_bzbz.AppendText("发卡地点：" + cardadd  + "\r\n");
                    txt_bzbz.AppendText("计划标志：" + plan_code + "\r\n");
                    txt_bzbz.AppendText("备注：" + bzbz  + "\r\n");
                }
                else
                {
                    txt_truckno.Text = truckno;
                    txt_bzbz.AppendText("没有该卡的动态信息！\r\n");
                }

            }
            else
            {
                string sqlStr = "select drivername,driverid,driverphone,stackname,cargoname,shipper_name,yard,truckcolor,cardtime,cardadd,plan_code from truck_login where TRUCKNO='" + truckno + "' AND IOYARDID='" + ioyardid + "'";
                //MessageBox.Show(StrSql);
                DataSet ds = ws.getDataSet_Serv(sqlStr);
                //DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sqlStr.ToString())).Tables[0];
                //DataSet ds = db.ExecuteDataSet(db.GetSqlStringCommand(sqlStr.ToString()));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    drivername = ds.Tables[0].Rows[0][0].ToString();
                    driverid = ds.Tables[0].Rows[0][1].ToString();
                    driverphone = ds.Tables[0].Rows[0][2].ToString();
                    stackname = ds.Tables[0].Rows[0][3].ToString();
                    cargoname = ds.Tables[0].Rows[0][4].ToString();
                    shipper_name = ds.Tables[0].Rows[0][5].ToString();
                    yard = ds.Tables[0].Rows[0][6].ToString();
                    truckcolor = ds.Tables[0].Rows[0][7].ToString();
                    cardtime = ds.Tables[0].Rows[0][8].ToString();
                    cardadd = ds.Tables[0].Rows[0][9].ToString();
                    plan_code = ds.Tables[0].Rows[0][10].ToString();

                    txt_truckno.Text = truckno;
                    txt_truckcolor.Text = truckcolor;
                    txt_id.Text = driverid;
                    txt_phone.Text = driverphone;
                    txt_drivername.Text = drivername;

                    txt_bzbz.AppendText("货名：" + cargoname + "\r\n");
                    txt_bzbz.AppendText("垛名：" + stackname + "\r\n");
                    txt_bzbz.AppendText("作业场区：" + yard + "\r\n");
                    txt_bzbz.AppendText("写卡时间：" + cardtime  + "\r\n");
                    txt_bzbz.AppendText("发卡地点：" + cardadd  + "\r\n");
                    txt_bzbz.AppendText("计划标志：" + plan_code  + "\r\n");
                }
                else
                {
                    txt_truckno.Text = truckno;
                    txt_bzbz.AppendText("没有该卡的动态信息！\r\n");
                }

            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void Frm_readcard_FormClosed(object sender, FormClosedEventArgs e)
        {
            //disconnect_card();
        }

        private void write_ini()
        {
            FileStream aFile = new FileStream(@"C:\truck_login_id.ini", FileMode.Open);
            StreamWriter sw = new StreamWriter(aFile);
            sw.WriteLine("1");
            sw.Close();
        }
    }
}
